﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Xml;

namespace Savage.SecureData
{
    internal class DataAccess
    {
        internal DataAccess(string connectionString)
        {
            ConnectionString = connectionString;
        }

        string ConnectionString { get; set; }

        internal void Save(Securable securable)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter p = new SqlParameter("@SecurableId", SqlDbType.UniqueIdentifier);
            p.Direction = ParameterDirection.InputOutput;
            p.Value = securable.SecurableId;
            parameters.Add(p);
            
            parameters.Add(new SqlParameter("@SchemaName", securable.SchemaName));
            parameters.Add(new SqlParameter("@DocumentType", securable.DocumentType.ToString()));
            parameters.Add(new SqlParameter("@Data", new SqlXml(new XmlTextReader(securable.Data.ToString(), XmlNodeType.Document, null))));

            DotNetLibrary.DatabaseLibrary.SqlDataWrapper sdw = new DotNetLibrary.DatabaseLibrary.SqlDataWrapper(ConnectionString);
            sdw.ExecuteNonQuery("Securable_Save", CommandType.StoredProcedure, parameters.ToArray());
            if (p.Value != null) 
                securable.SecurableId = (Guid) p.Value;
        }

        internal IDataReader FindXQuery(string schemaName, DocumentType documentType, string xquery)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SchemaName", schemaName));
            parameters.Add(new SqlParameter("@DocumentType", documentType.ToString()));
            parameters.Add(new SqlParameter("@XQuery", xquery));

            DotNetLibrary.DatabaseLibrary.SqlDataWrapper sdw = new DotNetLibrary.DatabaseLibrary.SqlDataWrapper(ConnectionString);
            return sdw.ExecuteReader("Securable_FindXQuery", CommandType.StoredProcedure, parameters.ToArray());
        }

        internal IDataReader FindText(string schemaName, string text)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SchemaName", schemaName));
            parameters.Add(new SqlParameter("@Text", text));

            DotNetLibrary.DatabaseLibrary.SqlDataWrapper sdw = new DotNetLibrary.DatabaseLibrary.SqlDataWrapper(ConnectionString);
            return sdw.ExecuteReader("Securable_FindText", CommandType.StoredProcedure, parameters.ToArray());
        }

        internal IDataReader Get(Guid securableId)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SecurableId", securableId));

            DotNetLibrary.DatabaseLibrary.SqlDataWrapper sdw = new DotNetLibrary.DatabaseLibrary.SqlDataWrapper(ConnectionString);
            return sdw.ExecuteReader("Securable_Get", CommandType.StoredProcedure, parameters.ToArray());
        }
    }
}
